package com.wenwei.sharebox.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.bean.WeChatPayConfigBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/12.
 *
 * 支付
 */
public class PayPopupWindow extends PopupWindow implements View.OnClickListener{

    Activity mActivity;
    PopupWindow popupWindow;


    ImageView backImg;
    ImageView weChatImg;
    ImageView aLiImg;

    RelativeLayout weChatRl;
    RelativeLayout aLiRl;

    TextView payPriceTv;
    TextView payCommitTv;

    private String mOrderNum ;

    public PayPopupWindow(Activity activity, View parentView,CallBack callBack,String orderNum) {
        mOrderNum = orderNum;
        this.mCallBack = callBack;
        mActivity = activity;
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.custom_pay_popup, null);

        backImg = (ImageView) contentView.findViewById(R.id.back_img);
        backImg.setOnClickListener(this);
        weChatImg = (ImageView) contentView.findViewById(R.id.pay_wechat_iv);
        aLiImg = (ImageView) contentView.findViewById(R.id.pay_aLi_iv);

        weChatRl = (RelativeLayout) contentView.findViewById(R.id.pay_wechat_rl);
        weChatRl.setOnClickListener(this);
        aLiRl = (RelativeLayout) contentView.findViewById(R.id.pay_ali_rl);
        aLiRl.setOnClickListener(this);

        payPriceTv = (TextView) contentView.findViewById(R.id.pay_price);
        payCommitTv = (TextView) contentView.findViewById(R.id.input_ok);
        payCommitTv.setOnClickListener(this);

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        ColorDrawable cd = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cd);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.4f;
        activity.getWindow().setAttributes(lp);
        popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }

    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }

    public CallBack mCallBack;

    public interface CallBack{
        void onPayClick();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_img:
                popupWindow.dismiss();
                break;
            case R.id.pay_wechat_rl:
                weChatImg.setImageResource(R.drawable.btn_selected);
                aLiImg.setImageResource(R.drawable.btn_xuanze);
                break;
            case R.id.pay_ali_rl:
                weChatImg.setImageResource(R.drawable.btn_xuanze);
                aLiImg.setImageResource(R.drawable.btn_selected);
                break;
            case R.id.input_ok:
//                mCallBack.onPayClick();
                weChatConfig();
                break;
        }
    }



    //微信支付签名
    BaseSubscriber<WeChatPayConfigBean> weChatSubscriber;
    private void weChatConfig(){
        weChatSubscriber = new BaseSubscriber<WeChatPayConfigBean>(mActivity) {
            @Override
            public void onSuccess(WeChatPayConfigBean weChatPayConfigBean) {
                Toast.makeText(mActivity,"微信支付签名获取成功",Toast.LENGTH_SHORT).show();
                weChatPayStart(weChatPayConfigBean);
//                mCallBack.onPayClick();
            }
        };
        ApiManager.getInstance().wxPayConfig(weChatSubscriber,mOrderNum);
    }

    private void weChatPayStart(WeChatPayConfigBean weChatPayBean) {
        String app_id = weChatPayBean.getAppid();
//        XiuApplication.APPID = app_id;
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(mActivity, app_id);
        iwxapi.registerApp(app_id);
        PayReq payRequest = new PayReq();
        payRequest.appId = app_id;
        payRequest.partnerId = weChatPayBean.getPartnerid();
        payRequest.prepayId = weChatPayBean.getPrepayid();
        payRequest.nonceStr = weChatPayBean.getNoncestr();
        payRequest.timeStamp = weChatPayBean.getTimestamp();
        payRequest.sign = weChatPayBean.getSign();
        payRequest.packageValue = "Sign=WXPay";
        Logger.d("appID:"+app_id+",partnerId:"+weChatPayBean.getPartnerid()+",prepayId:"+weChatPayBean.getPrepayid()+",nonceStr:"+weChatPayBean.getNoncestr()
                +",timeStamp:"+weChatPayBean.getTimestamp()+",sign:"+weChatPayBean.getSign()+".packageValue:Sign=WXPay");
        //发起请求
        iwxapi.sendReq(payRequest);
//        XiuApplication.WeChatPayIsSuccess = true;
    }
}
